﻿using MVC.OA.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MVC.OA.DALFactory;

namespace MVC.OA.BLL
{
    public abstract class BaseBLL<T> where T:class,new()
    {
        public IBaseDAL<T> CurrentDAL { get; set; }
        public IDbSession DbSession { get; set; }
        //public IDbSession DbSession
        //{
        //    get
        //    {
        //        return DbSessionFactory.GetCurrentDbSession();
        //    }
        //}

       // public abstract void SetCurrentDAL();
        #region CRUD
        public IQueryable<T> GetEntities(Expression<Func<T, bool>> selectWhere)
        {
            return CurrentDAL.GetEntities(selectWhere);
        }
        public IQueryable<T> GetPageEntities<S>(int pageSize, int pageIndex, out int total,
            Expression<Func<T, bool>> selecrWhere,
            Expression<Func<T, S>> orderByLamda, bool isAsc)
        {
            return CurrentDAL.GetPageEntities(pageSize, pageIndex, out total, selecrWhere, orderByLamda, isAsc);
        }

        public bool Add(T entity)
        {
            CurrentDAL.Add(entity);
            return DbSession.SaveChanges() > 0;
        }
        public bool Update(T entity)
        {
            CurrentDAL.Update(entity);
            return DbSession.SaveChanges() > 0;
        }
        public bool Delete(T entity)
        {
            CurrentDAL.Delete(entity);
            return DbSession.SaveChanges() > 0;
        }
        #endregion
    }
}
